package com.wy.dao;

import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wy.domain.AfficheForm;
import com.wy.tool.JDBConnection;

public class AfficheDao {
	private Connection connection = null;
	private PreparedStatement ps = null;
	private JDBConnection jdbc = null;

	public AfficheDao() {
		jdbc = new JDBConnection();
		connection = jdbc.connection;
	}

	public void deleteAffiche(Integer id) {
		try {
			ps = connection
					.prepareStatement("delete from tb_affiche where id = ?");
			ps.setInt(1, id.intValue());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException ex) {

		}
	}

	public void updateAffiche(AfficheForm form) {
		try {
			ps = connection
					.prepareStatement("update tb_affiche set name=?,content=? where id=?");
			ps.setString(1, form.getName());
			ps.setString(2, form.getContent());
			ps.setInt(3, form.getId().intValue());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException ex) {
		}
	}

	public void insertAffiche(AfficheForm form) {
		try {
			ps = connection
					.prepareStatement("insert into tb_affiche values(?,?,getDate())");
			ps.setString(1, form.getName());
			ps.setString(2, form.getContent());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException ex) {

		}
	}

	public AfficheForm selectOneAffiche(Integer id) {
		AfficheForm affiche = null;
		try {
			ps = connection
					.prepareStatement("select * from tb_affiche where id=?");
			ps.setInt(1, id.intValue());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				affiche = new AfficheForm();
				affiche.setId(Integer.valueOf(rs.getString(1)));
				affiche.setName(rs.getString(2));
				affiche.setContent(rs.getString(3));
				affiche.setIssueTime(rs.getString(4));
			}
		} catch (SQLException ex) {

		}
		return affiche;
	}

	public List selectAffiche() {
		List list = new ArrayList();
		AfficheForm affiche = null;
		try {
			ps = connection
					.prepareStatement("select * from tb_affiche order by id DESC");
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				affiche = new AfficheForm();
				affiche.setId(Integer.valueOf(rs.getString(1)));
				affiche.setName(rs.getString(2));
				affiche.setContent(rs.getString(3));
				affiche.setIssueTime(rs.getString(4));
				list.add(affiche);
			}
		} catch (SQLException ex) {

		}
		return list;
	}
}
